import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store";

Vue.use(VueRouter);

//解决重复路由报错问题 --重写push 方法
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
const routes = [
  {
    path: "",
    redirect: "/login",
  },
  {
    path: "/home",
    component: () => import("@/views/Home.vue"),
    children: [
      {
        path: "",
        redirect: "uploadTab",
      },
      {
        path: "uploadTab",
        component: () => import("@/components/home/upload/UploadTab.vue"),
      },
      {
        path: "allphoto",
        component: () => import("@/components/home/allphoto/AllPhoto.vue"),
      },
      {
        name: "photoshow",
        path: "photoshow",
        component: () => import("@/components/public/PhotoesShow.vue"),
      },
      {
        path: "private",
        component: () => import("@/components/home/private/Private.vue"),
      },
      {
        path: "mePhoto",
        component: () => import("@/components/home/mephoto/MePhoto.vue"),
      },
      {
        path: "recycle",
        component: () => import("@/components/home/recycle/Recycle.vue"),
      },
      {
        path: "smartSort",
        component: () => import("@/components/home/smartAblum/SmartSort.vue"),
      },
      {
        path: "showSamrt",
        name: "showSamrt",
        component: () => import("@/components/home/smartAblum/ShowSmart.vue"),
      },
      {
        path: "clip",
        name: "clip",
        component: () => import("@/components/home/clip/Clip.vue"),
      },
      {
        path: "personalCenter",
        component: () =>
          import("@/components/home/Personal/PersonalCenter.vue"),
        children: [
          {
            path: "",
            redirect: "analysisHabit",
          },
          {
            path: "analysisHabit",
            component: () =>
              import("@/components/home/Personal/AnalysisHabit.vue"),
          },
          {
            path: "meFollow",
            component: () => import("@/components/home/Personal/Mefollow.vue"),
          },
          {
            path: "meCollection",
            component: () =>
              import("@/components/home/Personal/MeCollection.vue"),
          },
          {
            path: "meShare",
            component: () => import("@/components/home/Personal/MeShare.vue"),
          },
        ],
      }, //Personal/PersonalCenter
    ],
  },
  {
    path: "/option",
    component: () => import("@/views/Option.vue"),
  },
  {
    path: "/login",
    component: () => import("@/views/Login.vue"),
  },
  {
    path: "/shareView/:shareId",
    component: () => import("@/views/ShareView.vue"),
  },
  {
    path: "/square",
    component: () => import("@/components/home/Personal/Square.vue"),
  },
  {
    path: "/userMessage",
    component: () => import("@/components/home/Personal/UserMessage.vue"),
    children: [
      {
        path: "",
        redirect: "analysisHabit",
      },
      {
        path: "analysisHabit",
        component: () => import("@/components/home/Personal/AnalysisHabit.vue"),
      },
      {
        path: "meCollection",
        component: () => import("@/components/home/Personal/MeCollection.vue"),
      },
      {
        path: "meShare",
        component: () => import("@/components/home/Personal/MeShare.vue"),
      },
    ],
  },
  {
    path: "/phone",
    component: () => import("@/views/Phone.vue"),
  },
];

const route = [
  {
    path: "",
    redirect: "/login",
  },
  {
    path: "/login",
    component: () => import("@/views/Option.vue"),
  },
];

const router = new VueRouter({
  // routes,
  route,
});

router.beforeEach((to, from, next) => {
  // console.log(to.fullPath)
  if (
    store.state.token ||
    to.fullPath === "/login" ||
    to.fullPath.split("/")[1] === "shareView" ||
    to.fullPath === "/phone"
  ) {
    next();
  } else {
    next();
    // next('/login')
  }
});

export default router;
